﻿using System;

using System.Collections.Generic;
//using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using SmartMovieTime.Properties;

namespace SmartMovieTime
{
    public partial class ConfigForm : Form
    {
        class AreaConfigItem
        {
            private string name;

            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private string id;

            public string Id
            {
                get { return id; }
                set { id = value; }
            }
            private bool enabled;

            public bool Enabled
            {
                get { return enabled; }
                set { enabled = value; }
            }

            public AreaConfigItem(string name, string id, bool enabled)
            {
                this.name = name;
                this.id = id;
                this.enabled = enabled;
            }
        }

        private Dictionary<string, AreaConfigItem> configTable;
        private string dataDir;
        

        public ConfigForm(string dataDir)
        {
            InitializeComponent();

            this.dataDir = dataDir;
            configTable = new Dictionary<string,AreaConfigItem>();
            InitConfigTable();
            
            string areaTableFile = AtMovieTimeExtractor.GetAreaTableFile(dataDir);
            if (File.Exists(areaTableFile))
            {
                LoadConfigFile(areaTableFile);
            }

            foreach (AreaConfigItem item in configTable.Values)
            {
                ListViewItem lvi = new ListViewItem(item.Name);
                lvi.Checked = item.Enabled;
                lvUpdateArea.Items.Add(lvi);
            }
            
        }

        private void InitConfigTable()
        {
            AreaConfigItem item = new AreaConfigItem("基隆", "a01", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("台北", "a02", true);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("桃園", "a03", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("苗栗", "a37", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("台中", "a04", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("彰化", "a47", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("雲林", "a45", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("嘉義", "a05", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("台南", "a06", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("高雄", "a07", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("屏東", "a87", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("宜蘭", "a39", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("南投", "a49", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("花蓮", "a38", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("台東", "a89", false);
            configTable.Add(item.Name, item);
            item = new AreaConfigItem("澎湖", "a69", false);
            configTable.Add(item.Name, item);

        }

        private void LoadConfigFile(string areaTableFile)
        {
            TxtTable<AtMovieAreaRow> areaTable = new TxtTable<AtMovieAreaRow>();
            areaTable.LoadFromFile(areaTableFile);
            AtMovieAreaRow areaRow = null;
            while ((areaRow = areaTable.ReadNext()) != null)
            {
                if (configTable.ContainsKey(areaRow.AreaName))
                {
                    configTable[areaRow.AreaName].Enabled = areaRow.Enabled;
                }
            }
            areaTable.Close();
        }

        private void OutputConfigFile()
        {
            const char Delimiter = ',';

            string areaTableFile = AtMovieTimeExtractor.GetAreaTableFile(dataDir);
            StreamWriter writer = new StreamWriter(areaTableFile, false, Encoding.Default);
            foreach (AreaConfigItem item in configTable.Values)
            {

                writer.WriteLine("{0}{3}{1}{3}{2}", item.Name, item.Id, (item.Enabled)?"1":"0", Delimiter);
            }
            writer.Close();
            
           
        }

        private void menuOk_Click(object sender, EventArgs e)
        {
            //Clear old data files
            string[] oldDataFiles = Directory.GetFiles(dataDir, "*.txt");
            foreach (string dataFile in oldDataFiles)
            {
                File.Delete(dataFile);
            }

            //Generate new area list
            foreach (ListViewItem lvi in lvUpdateArea.Items)
            {
                configTable[lvi.Text].Enabled = lvi.Checked;
            }

            OutputConfigFile();

            //Update movie time
            Cursor oldCursor = Cursor.Current;
            bool success = false;
            try
            {

                Cursor.Current = Cursors.WaitCursor;
                AtMovieTimeExtractor atMovieTime = new AtMovieTimeExtractor(dataDir);
                atMovieTime.UpdateMovieTime();
                success = true;
            }
            catch (SystemException ex)
            {
                //Show error message.
                MessageBox.Show(ex.Message, Resources.UpdateErrorTitle, MessageBoxButtons.OK
                    , MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                success = false;
            }

            Cursor.Current = oldCursor;

            //Return result
            this.DialogResult = (success)?DialogResult.OK:DialogResult.Abort;

            Close();

        }

        private void menuCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}